﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using IPhonePlugin;

namespace NguyenKimPhonePlugin
{
    public class NguyenKimPhonePlugin:IPhonePlugin.IPhonePlugin
    {

        public List<PhoneInfo> getPhone(string name, int nItem)
        {
            var webGet = new HtmlWeb();
            string temp = name.Trim();
            temp = temp.Replace(' ', '+');
            List<PhoneInfo> listPhone = new List<PhoneInfo>();
            string url = "http://www.nguyenkim.com/?dispatch=products.search&q=" + temp + "&subcats=Y&status=A&pname=Y&search_performed=Y";
            var document = webGet.Load(url);
            if (document != null)
            {
                HtmlNodeCollection Nodes = document.DocumentNode.SelectNodes("//div[@class='block_right_cate2_new_ver2']");
                if (Nodes != null && Nodes.Count > 0)
                {
                    int n = Nodes.Count;
                    if (n > nItem)
                    {
                        n = nItem;
                    }
                    for (int i = 0; i < n; i++)
                    {
                        PhoneInfo PhoneInfo = new PhoneInfo();
                        PhoneInfo.CuaHang = "Nguyễn Kim";

                        HtmlNode nodeTen = Nodes[i].SelectSingleNode("child::div[@class='block_title_sp_home_new']/a[@class='product-title']");  

                        HtmlNodeCollection list= nodeTen.ChildNodes;
                        if (nodeTen != null && nodeTen.InnerText != null)
                            PhoneInfo.TenSanPham = nodeTen.InnerText.Trim();

                        HtmlNode nodeGia = Nodes[i].SelectSingleNode("child::div[@class='block_price_sp_home_new']");
                        if (nodeGia != null && nodeGia.InnerText != null)
                            PhoneInfo.GiaBan = nodeGia.InnerText.Trim().Replace("&nbsp;VND","");

                        listPhone.Add(PhoneInfo);
                    }
                }
            }

            return listPhone;
        }
    }
}
